import java.io.*;
import java.net.*;


public class eClient {
	
	public static void main(String argv[]) throws Exception
	

	{
		//String sentence;
		String modifiedSentence;
		File file = new File("LOG_" + System.currentTimeMillis() + ".txt");
		Writer output = null;
		//BufferedReader inFromUser = new BufferedReader(
			//	new InputStreamReader(System.in));
		Socket clientSocket = new Socket("127.0.0.1", 6789);
		DataOutputStream outToServer = new DataOutputStream(
				clientSocket.getOutputStream());
		BufferedReader inFromServer =
			new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));

		System.out.println("Type Message...");
		String message;
		BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

		while((message = console.readLine())!= null){
			//sentence = inFromUser.readLine();
			if (message.equals("quit")){
				System.out.println("Bye-Bye");
				break;
			}

			outToServer.writeBytes(message + '\n');
			modifiedSentence = inFromServer.readLine();
			System.out.println("FROM SERVER:" +
					modifiedSentence);


			output = new BufferedWriter(new FileWriter(file, true));
			output.write(modifiedSentence);
			System.out.println("Your file has been written.");
			((BufferedWriter) output).newLine();
			output.flush();
			output.close();
			//main(argv);
			
		}
		clientSocket.close();
		System.out.println("Client Socket Closed.");
		System.exit(1);
	}
	
}

